/********************************************************************************
 * @Author: Zhenzhou Liu
 * @Date: 2024-12-05
 * @Description: 
 *******************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>

using namespace std;

struct Activity {
    string name;
    int startTime;
    int endTime;
    int cost;
    Activity(string name, int startTime, int endTime, int cost): name(name), startTime(startTime),
    endTime(endTime), cost(cost) {}
};

int binarySearch(const vector<Activity>& activities, int left, int right, const int& target) {
    int index = 0;
    while (left <= right) {
        int midden = left + ((right - left) >> 1);
        if (activities[midden].endTime <= target) {
            index = midden;
            left = midden + 1;
        } else {
            right = midden - 1;
        }
    }
    return index;
}

void maxActivitiesWithWeight(vector<Activity>& activities, int& beginTime, int& closeTime, stack<string>& result) {
    sort(activities.begin(), activities.end(), [](const Activity& a, const Activity& b) {
        return a.endTime < b.endTime;
    });
    int n = activities.size();
    activities.insert(activities.begin(), Activity("null", 0, 0, 0));
    vector<int> p(n + 1, 0);
    for (int i = 1; i <= n; ++i) {
        p[i] = binarySearch(activities, 1, i - 1, activities[i].startTime);
    }

    vector<int> dp(n + 1, 0);
    vector<bool> record(n + 1, false);
    for (int i = 1; i <= n; ++i) {
        int temp = dp[p[i]] + activities[i].cost;
        if (temp > dp[i - 1]) {
            dp[i] = temp;
            record[i] = true;
        } else {
            dp[i] = dp[i - 1];
        }
    }

    int k = n;
    while (k > 0) {
        if (record[k]) {
            result.push(activities[k].name);
            k = p[k];
        } else {
            k -= 1;
        }
    }
}


int main() {
    string line;
    int beginTime, closeTime;
    vector<Activity> activities;
    while (getline(cin, line) && !line.empty()) {
        if (line[0] =='C') {
            size_t minusPos = line.find('-');
            beginTime = stoi(line.substr(2, minusPos - 2));
            closeTime = stoi(line.substr(minusPos + 1));
            break;
        }

        size_t colonPos = line.find(':');
        size_t firstCommaPos = line.find(',');
        size_t secondCommaPos = line.find(',', firstCommaPos + 1);
        if (colonPos != string::npos && firstCommaPos != string::npos && secondCommaPos != string::npos) {
            activities.push_back(Activity(line.substr(0, colonPos), stoi(line.substr(colonPos + 1, firstCommaPos - colonPos - 1)),
                                                              stoi(line.substr(firstCommaPos + 1,
                                                                               secondCommaPos - firstCommaPos - 1)),
                                                              stoi(line.substr(secondCommaPos + 1))));
        }
    }

    stack<string> result;
    maxActivitiesWithWeight(activities, beginTime, closeTime, result);
    while (!result.empty()) {
        cout << result.top();
        result.pop();
        if (!result.empty()) {
            cout << ',';
        }
    }
    return 0;
}
/*

Test Case1:
a1:3,6,2
a2:4,8,10
a3:6,9,5
C:2-10
*/
